﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Web;

namespace CNVP.Config
{
    /// <summary>
    /// 配置信息操作底层类
    /// </summary>
    public class BaseXmlCtrl
    {
        /// <summary>
        /// 全局XML文档对象
        /// </summary>
        private XmlDocument xml = new XmlDocument();
        /// <summary>
        /// 全局完整后缀配置文件保存的服务器路径
        /// </summary>
        private String xmlPath =String.Empty;
        /// <summary>
        /// 全局完整后缀配置文件保存的服务器路径，只写属性
        /// </summary>
        public String XmlPath 
        {
            set {
                this.xmlPath = HttpContext.Current.Server.MapPath(@"~/Config/" + value + @".config");
            }
        }
        /// <summary>
        /// 读取指定项目下的值
        /// </summary>
        /// <param name="itemName">带路径项目名</param>
        /// <returns>值</returns>
        public String GetSingle(String itemName)
        {
            String g = String.Empty;
            if (!String.IsNullOrEmpty(xmlPath))
            {
                try
                {
                    xml.Load(xmlPath);
                    XmlNodeList rootList = xml.SelectNodes(itemName);
                    if (rootList.Count > 0)
                    {
                        XmlNode xn = rootList[0];
                        g = Convert.ToString(xn.InnerText);
                    }
                }
                catch
                {
                    return String.Empty;
                }
            }
            return g;
        }
        /// <summary>
        /// 保存配置信息
        /// </summary>
        /// <param name="strTarget">目标标签</param>
        /// <param name="strValue">标签值</param>
        public void SaveXmlConfig(string strTarget, string strValue)
        {
            if (!String.IsNullOrEmpty(xmlPath))
            {
                try
                {
                    xml.Load(xmlPath);
                    XmlElement root = xml.DocumentElement;
                    XmlNodeList elemList = root.GetElementsByTagName(strTarget);
                    if (elemList.Count > 0)
                    {
                        elemList[0].InnerText = strValue;
                        xml.Save(xmlPath);
                    }
                }
                catch
                { }
            }
        }
        /// <summary>
        /// 刷新缓存
        /// </summary>
        /// <param name="strTarget">目标标签</param>
        /// <returns>值</returns>
        public String GetCatchParam(String strTarget)
        {
            if (!String.IsNullOrEmpty(xmlPath))
            {
                try
                {
                    xml.Load(xmlPath);
                    XmlElement root = xml.DocumentElement;
                    XmlNodeList elemList = root.GetElementsByTagName(strTarget);
                    if (elemList.Count > 0)
                    {
                        return elemList[0].InnerText;
                    }
                    else
                    {
                        return String.Empty;
                    }
                }
                catch
                {
                    return String.Empty;
                }
            }
            else
            {
                return String.Empty;
            }
        }
    }
}
